package com.dev.sacot41.scviewpager;

import ohos.agp.components.AttrSet;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Samuel on 2015-06-27.
 */
public class DotsView extends DirectionalLayout {

    private List<Image> dots;
    private int numberOfPage = 0;
    private int mSelectedRessource;
    private int mUnSelectedRessource;

    public DotsView(Context context) {
        this(context, null);
    }

    public DotsView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public DotsView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        setOrientation(HORIZONTAL);
    }

    /**
     * 设置指示器选中和未选中时显示的图片资源id
     *
     * @param selectedRessource   选中时显示的图片资源id
     * @param unSelectedRessource 未选中时显示的图片资源id
     */
    public void setDotRessource(int selectedRessource, int unSelectedRessource) {
        mSelectedRessource = selectedRessource;
        mUnSelectedRessource = unSelectedRessource;
    }

    public void setNumberOfPage(int pageNumber) {
        numberOfPage = pageNumber;
        dots = new ArrayList<>();
        for (int i = 0; i < numberOfPage; i++) {
            Image dot = new Image(getContext());
            dot.setPixelMap(mUnSelectedRessource);
            LayoutConfig params = new LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
            addComponent(dot, params);
            dots.add(dot);
        }
        selectDot(0);
    }

    /**
     * 设置当前选中条目
     *
     * @param idx 当前选中条目
     */
    public void selectDot(int idx) {
        for (int i = 0; i < numberOfPage; i++) {
            int drawableId = (i == idx) ? (mSelectedRessource) : (mUnSelectedRessource);
            dots.get(i).setPixelMap(drawableId);
        }
    }

}
